package ciyuan.Gui.Ornaments;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Gui.Slot.SkillSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.IContainerListener;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class OrnamentsContainer extends Container {

    private final Inventory inventory = new Inventory(3);
    private PlayerEntity player = null;

    public OrnamentsContainer(@Nullable ContainerType<?> containerType, int id, PlayerInventory playerInventory) {
        super(containerType, id);
        addSlot(new SkillSlot(inventory, 0, 10 - 62, 11));
        addSlot(new SkillSlot(inventory, 1, 10 - 62, 33));
        addSlot(new SkillSlot(inventory, 2, 10 - 62, 54));
        addSlotListener(new IContainerListener() {
            @Override
            public void refreshContainer(Container container, NonNullList<ItemStack> itemStacks) {
                OrnamentsContainer ornamentsContainer = (OrnamentsContainer) container;
                ornamentsContainer.setItem();
            }

            @Override
            public void slotChanged(Container container, int index, ItemStack itemStack) {
                if (player != null) {
                    if (index == 0) {
                        PlayerDataManager.setServerPlayerData(player, PlayerDataType.SKILL_SLOT1, itemStack);
                    } else if (index == 1) {
                        PlayerDataManager.setServerPlayerData(player, PlayerDataType.SKILL_SLOT2, itemStack);
                    } else if (index == 2) {
                        PlayerDataManager.setServerPlayerData(player, PlayerDataType.SKILL_SLOT3, itemStack);
                    }
                }
            }

            @Override
            public void setContainerData(Container container, int p_71112_2_, int p_71112_3_) {
            }
        });
        layoutPlayerInventorySlots(playerInventory, 8, 84);
    }

    public void setItem() {
        inventory.setItem(0, PlayerDataManager.getItemStack(player, PlayerDataType.SKILL_SLOT1));
        inventory.setItem(1, PlayerDataManager.getItemStack(player, PlayerDataType.SKILL_SLOT2));
        inventory.setItem(2, PlayerDataManager.getItemStack(player, PlayerDataType.SKILL_SLOT3));
    }

    @Override
    public boolean stillValid(PlayerEntity player) {
        this.player = player;
        return true;
    }

    private void layoutPlayerInventorySlots(IInventory inventory, int leftCol, int topRow) {
        // Player inventory
        addSlotBox(inventory, 9, leftCol, topRow, 9, 18, 3, 18);

        // Hotbar
        topRow += 58;
        addSlotRange(inventory, 0, leftCol, topRow, 9, 18);
    }

    private int addSlotBox(IInventory inventory, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
        for (int j = 0; j < verAmount; j++) {
            index = addSlotRange(inventory, index, x, y, horAmount, dx);
            y += dy;
        }
        return index;
    }

    private int addSlotRange(IInventory inventory, int index, int x, int y, int amount, int dx) {
        for (int i = 0; i < amount; i++) {
            addSlot(new Slot(inventory, index, x, y));
            x += dx;
            index++;
        }
        return index;
    }

    @Override
    public ItemStack quickMoveStack(PlayerEntity player, int index) {
        return ItemStack.EMPTY;
    }


}
